package com.hs.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hs.entity.Reply;
import com.hs.entity.User;
import com.hs.mapper.UserMapper;
import com.hs.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public int addUser(User user) {
        user.setRegisterTime(new Date(System.currentTimeMillis()));
        user.setAddress("河南省开封市");
        user.setPoint(100);
        return userMapper.addUser(user);
    }

    @Override
    public int deleteUser(int id) {
        return userMapper.deleteUser(id);
    }

    @Override
    public int updateUser(User user) {

        return userMapper.updateUser(user);
    }

    @Override
    public User getUserById(int id) {
        return userMapper.getUserById(id);
    }

    @Override
    public PageInfo<User> getUserByPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<User> users = userMapper.getUserByPage();
        PageInfo<User> pageInfo = new PageInfo<>(users);
        return pageInfo;
    }

    @Override
    public User getUserByLogin(String account, String password) {
        return userMapper.getUserByLogin(account, password);
    }

    @Override
    public int updateUserGai(User user) {
        String account;
        // 如果用户未提供账号，从数据库查询当前账号（保持原有逻辑）
        if (user.getAccount() == null || user.getAccount().isEmpty()) {

            account = userMapper.selectUser(user.getId()).getAccount();
        } else {
            account = user.getAccount();
        }

        // 移除密码加密逻辑，直接使用用户提供的密码
        // 注意：这里保留了密码为空时的处理（不更新密码）
        if (user.getPassword() != null && !user.getPassword().isEmpty()) {
            // 不进行加密，直接使用原始密码
        } else {
            user.setPassword(null);
        }

        return userMapper.updateUserGai(user);
    }
    private static final String PHONE_REGEX = "^1[3-9]\\d{9}$";
    @Override
    public User getUserByPhone(String phone) {
        return userMapper.getUserByPhone(phone);
    }

    @Override
    public String updatePhone(Long userId, String newPhone) {
        // 1. 参数合法性校验
        if (userId == null || userId <= 0) {
            return "用户ID不合法";
        }
        if (newPhone == null || newPhone.trim().isEmpty()) {
            return "新手机号不能为空";
        }

        // 2. 手机号格式校验
        if (!newPhone.matches(PHONE_REGEX)) {
            return "手机号格式不正确，请输入11位有效手机号";
        }

        // 3. 验证用户是否存在
        User user = userMapper.selectById(userId);
        if (user == null) {
            return "用户不存在";
        }

        // 4. 检查新手机号是否已被其他用户使用
        User existingUser = userMapper.getUserByPhone(newPhone);
        if (existingUser != null) {
            // 使用Objects.equals()避免空指针，且自动处理基础类型与包装类型的比较
            if (!Objects.equals(existingUser.getId(), userId)) {
                return "该手机号已被其他用户绑定";
            }
        }

        // 5. 检查是否与原手机号相同
        if (newPhone.equals(user.getPhone())) {
            return "新手机号与原手机号相同，无需修改";
        }

        // 6. 执行更新操作
        User updateUser = new User();
        updateUser.setId(Math.toIntExact(userId));
        updateUser.setPhone(newPhone);
        updateUser.setRegisterTime(new Date());

        int rows = userMapper.updateUser(updateUser);
        if (rows > 0) {
            return "success";
        } else {
            return "手机号更新失败，请重试";
        }
    }

    @Override
    public List<Reply> findReplyById(int id) {
        return userMapper.findReplyById(id);
    }

    @Override
    public PageInfo<Reply> findReplyByPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Reply> replies = userMapper.findReplyByPage();
        PageInfo<Reply> pageInfo = new PageInfo<>(replies);
        return pageInfo;
    }
}

